myApp.controller('addZoneAndAppController', function ($scope, $uibModal, $stateParams, ngDialog, $http, $filter, $interval, $timeout, $rootScope) {
    $scope.info = {};

    $scope.info.validate = true;
    $scope.info.addType = 'zone';

    $scope.info.zone = {};
    $scope.info.app = {};

    $scope.info.selectZoneList = [];

    $scope.tabClick = function (type) {
        $scope.info.addType = type;
        $scope.getZoneList();
    };

    $scope.getZoneList = function () {
        $http.get(ucloud_master_url + '/api/v1/zone/', {
            headers: {'Accept': 'application/json'}
        }).success(function (data) {
            $scope.info.selectZoneList = data.data;
            $scope.init();
        });
    };

    $scope.init = function(){
        $scope.info.zone = {};
        $scope.info.app = {};
        $scope.info.app.app_type = 'tomcat';
        $scope.info.app.on_beetle = 'true';
        $scope.info.app.zone_code = $scope.info.selectZoneList[0].zone_code;
    };

    $scope.getZoneList();

    $scope.save = function () {
        var url = '';
        var param = {};
        if ($scope.info.addType == 'zone') {
            url = ucloud_master_url + '/api/v1/zone/add';
            param = $scope.info.zone;
        } else {
            url = ucloud_master_url + '/api/v1/app/add';
            param = $scope.info.app;
        }
        param.token_id = getTokenId();
        $http.post(url, param).success(function (data) {
            if (data.success) {
                alert('添加成功');
                $scope.getZoneList();
            } else {
                alert(data.msg);
            }
            ;
        }).error(function (data) {
            alert(JSON.stringify(data.messages))
        });
    };

    $scope.goback = function () {
        window.location.href = '#/menu/zalist/';
    }
});